package com.chinaunicom.group.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import oracle.net.aso.e;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chinaunicom.group.model.GroupArea;
import com.chinaunicom.group.model.GroupBean;
import com.chinaunicom.group.model.GroupCity;
import com.chinaunicom.group.model.GroupData;
import com.chinaunicom.group.model.province.ProvinceBean;
import com.chinaunicom.group.service.IGroupAreaService;
import com.chinaunicom.group.service.IGroupBeanService;
import com.chinaunicom.group.service.IGroupCityService;
import com.chinaunicom.group.service.IGroupDataService;
import com.chinaunicom.group.service.IGroupProvinceService;
import com.chinaunicom.group.service.impl.GroupCityServiceImpl;

@Controller
@RequestMapping("/crm/group")
public class Groupcontroller 
{
	@Resource
	private IGroupBeanService groupBeanService;
	
	@Resource
	private IGroupProvinceService groupProvinceService;
	
	@Resource
	private IGroupDataService dataGroupService;
	
	@Resource
	private IGroupCityService groupCityService;
	
	@Resource
	private IGroupAreaService groupAreaService;
	
	/**
	 * 集团资料全部查询
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/groupdataquery")
	public Map<String, Object> groupdataquery()
	{
		//从数据库中查询集团数据
		List<GroupBean> grouplist = groupBeanService.queryAll(null);
		//将集团数据放入作用域中
		HashMap<String,Object> map = new HashMap<>();
		//System.out.println(grouplist.size()+"--------------");
		map.put("total", grouplist.size());
		map.put("rows", grouplist);
		return map;
	}
	
	
	@RequestMapping("/querydetail")
	public String dequerydetail(Model model,int id)
	{
		model.addAttribute("id", id);
		return "/WEB-INF/group/groups/groupdetail.jsp";
	}
	
	
	/**
	 * 单个集团信息查询
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/groupquerydetail")
	public Map<String, Object> juquerydetail(int id)
	{
		//从数据库查询单个集团数据
		GroupBean groupdetail = groupBeanService.queryByid(id);
		//将集团数据放入作用域中
		ArrayList<GroupBean> list = new ArrayList<>();
		list.add(groupdetail);
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("total", list.size());
		map.put("rows", list);
		return map;
	}
		
	
	
	/**
	 * 跳转至集团大体展示页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/jugroupdataquery")
	public String jgroupdataquery(Model model)
	{
		//查询所有省份
		List<ProvinceBean> provinces = groupProvinceService.queryAll(null);
		
		//查询城市
		List<GroupCity> citylist = groupCityService.queryAll();
		
		//查询地区
		List<GroupArea> arealist = groupAreaService.queryAll();
		
		//查询客户经理
		List<GroupData> managerlist = dataGroupService.queryManager();
		
		//查询集团状态
		List<GroupData> statuslist = dataGroupService.queryGroupStatusList();
		
		//查询所有行业
		List<String> industry = dataGroupService.queryIndustryAll();
		
		model.addAttribute("provinces",provinces);
		model.addAttribute("citylist", citylist);
		model.addAttribute("arealist", arealist);
		model.addAttribute("industry", industry);
		model.addAttribute("statuslist", statuslist);
		model.addAttribute("managerlist", managerlist);
		
		return "/WEB-INF/group/left/groupdataquery.jsp";
	}
	
	/**
	 * 集团资料更新
	 * @return
	 */
	@RequestMapping("/groupupdate")
	public String groupupdate(Model model,int id)
	{
		GroupBean group = groupBeanService.queryByid(id);
		//List<GroupBean> list = groupBeanService.queryAll(null);
		//查询集团省份
		List<ProvinceBean> provincelist = groupProvinceService.queryAll(null);
		//查询城市
		//List<GroupCity> citylist = groupCityService.queryAll();
		List<GroupCity> citylist = groupCityService.queryByGroupProvince(group.getGroupprovince());
		
		//通过城市名查询地区
		List<GroupArea> arealist = groupAreaService.queryByGroupCityName(group.getGroupcity());
		
		//查询集团等级
		List<String> level = dataGroupService.queryLevel();
		//查询集团财务状况
		List<String> finanstution = dataGroupService.queryFinanstution();
		//查询集团发展潜力
		List<String> grouppotential = dataGroupService.queryGroupPotential();
		//查询集团信用等级
		List<String> groupcredit = dataGroupService.queryGroupCredit();
		//查询集团状态(潜在)
		List<String> groupstatus = dataGroupService.queryGroupStatus();
		//查询集团文化水平
		List<String>groupeducation = dataGroupService.queryGroupEducation();
		
		model.addAttribute("groupmsg", group);
		model.addAttribute("provincelist",provincelist);
		model.addAttribute("citylist", citylist);
		model.addAttribute("arealist",arealist);
		model.addAttribute("level",level);
		model.addAttribute("finanstution", finanstution);
		model.addAttribute("grouppotential", grouppotential);
		model.addAttribute("groupcredit", groupcredit);
		model.addAttribute("groupstatus", groupstatus);
		model.addAttribute("groupeducation", groupeducation);
		return "/WEB-INF/group/left/groupupdate.jsp";
	}
	
	
	/**
	 * 根据省份id查询市
	 * @param id
	 * @throws UnsupportedEncodingException 
	 */
	@ResponseBody
	@RequestMapping("/getCity")
	public List<GroupCity> getCity(String provincename) 
	{
		//根据传递过来的provincename查询出该省份信息
		ProvinceBean groupProvince = groupProvinceService.queryByProvinceName(provincename);
		
		//根据省份id查询出省份下面所有的城市
		List<GroupCity> groupCity = groupCityService.queryByProvinceId(groupProvince.getProvinceid());
		
		return groupCity;
	}
	
	/**
	 * 根据城市id查询地区
	 * @param cityid
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getArea")
	public List<GroupArea> getArea(String cityname) 
	{
		//根据城市名称获取城市信息
		GroupCity groupCity = groupCityService.queryByCityName(cityname);
		
		//根据城市id查询出城市下面所有的地区
		List<GroupArea> groupArea = groupAreaService.queryByCityId(groupCity.getCityid());
		
		return groupArea;
	}
	
	
	
	/**
	 * 提交修改后的表单
	 * @param groupBean
	 * @return
	 */
	@RequestMapping("/groupupdatesubmit")
	public String groupupdatesubmit(GroupBean groupBean)
	{
		//更新数据库集团数据
		groupBeanService.groupupdatesubmit(groupBean);
		return "/crm/group/groupupdate?id="+groupBean.getGroupid();
	}
	
	/**
	 * 高级检索
	 * @param groupBean
	 */
	@ResponseBody
	@RequestMapping("/advancedsearch")
	public Map<String, Object> advancedsearch(GroupBean groupBean)
	{
		
		List<GroupBean> grouplist = groupBeanService.advancedSearch(groupBean);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("total", grouplist.size());
		map.put("rows", grouplist);
		
		return map;
	}
	
	/**
	 * 集团资料删除
	 * @return
	 */
	@RequestMapping("/groupdelete")
	public String groupdelete(int id)
	{
		groupBeanService.groupdelete(id);
		System.out.println(id+"-------------");
		return "/WEB-INF/group/left/groupdataquery.jsp";
	}
	
	/**
	 * 集团资料添加
	 */
	@RequestMapping("/groupinsert")
	public String groupinsert(Model model)
	{
		//查询集团省份
		List<ProvinceBean> provincelist = groupProvinceService.queryAll(null);
		//查询集团等级
		List<String> level = dataGroupService.queryLevel();
		//查询城市
		List<GroupCity> citylist = groupCityService.queryAll();
		//查询地区
		List<GroupArea> arealist = groupAreaService.queryAll();
		//查询集团发展潜力
		List<String> grouppotential = dataGroupService.queryGroupPotential();
		//查询集团财务状况
		List<String> finanstution = dataGroupService.queryFinanstution();
		//查询集团信用等级
		List<String> groupcredit = dataGroupService.queryGroupCredit();
		//查询集团状态(潜在)
		List<String> groupstatus = dataGroupService.queryGroupStatus();
		//查询集团文化水平
		List<String>groupeducation = dataGroupService.queryGroupEducation();
		
		model.addAttribute("provincelist", provincelist);
		model.addAttribute("level", level);
		model.addAttribute("citylist", citylist);
		model.addAttribute("arealist", arealist);
		model.addAttribute("grouppotential", grouppotential);
		model.addAttribute("finanstution", finanstution);
		model.addAttribute("groupcredit", groupcredit);
		model.addAttribute("groupstatus", groupstatus);
		model.addAttribute("groupeducation", groupeducation);
		
		return "/WEB-INF/group/groups/groupinsert.jsp";
	}
	
	/**
	 * 提交到数据库中
	 * @param groupBean
	 */
	@RequestMapping("/groupinsertsubmit")
	public String groupinsertsubmit(GroupBean groupBean)
	{
		int i = groupBeanService.insert(groupBean);
		return "/crm/group/jugroupdataquery";
	}
}
